package edu.neepu;

import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.junit.Test;
import sun.rmi.runtime.NewThreadAction;

import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

public class Main {

    public  void getInformation(int index) throws IOException, InterruptedException {
            Connection connect = Jsoup.connect("https://bing.ioliu.cn/?p=" + index).header("Accept-Language", "zh-cn").userAgent("Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/14.0.835.163 Safari/535.1").timeout(10000);
            Document document_html = connect.get();
            Elements elements_mark = document_html.select(".mark");
            for (Element element_EveryMark : elements_mark) {
                Element href = element_EveryMark.select("a[href]").first();
                String attr = href.attr("abs:href");
                Document document_nextHtml = Jsoup.connect(attr).timeout(10000).get();
                Elements elementsOption = document_nextHtml.select(".preview");
                for (Element element : elementsOption) {
                    Element element1_location = element.select(".description").first();
                    Element timeElement = element1_location.select(".calendari.icon.icon-calendar").first();
                    Element element_t = timeElement.select(".t").first();
                    Element locElement = element1_location.select(".location").first();
                    Element element_loc = locElement.select(".t").first();
                    //Element emText = emElement.select("em").first();
                    Element element1_download = element.select(".ctrl.download").first();
                    String attr_download = element1_download.attr("abs:href");
                    String fileName = element_t.text()+element_loc.text();
                    System.out.println(fileName);
                    while (true) {
                        synchronized (Config.indexLock) {
                            if (Config.index <= 5) {
                                new CreateFile(fileName, attr_download).start();
                                Config.index++;
                                break;
                            }
                        }
                        Thread.sleep(20);
                    }
                }
            }
    }
    public static void main(String[] args) throws IOException, InterruptedException {
        Main m = new Main();
        for (int i=1;i<=10;i++){
            m.getInformation(i);
        }

    }
}
